package com.webull.information.center.carwler.common.util.jsoup.prase_en;

import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.Optional;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;

import com.webull.framework.util.UtilDate;
import com.webull.information.center.carwler.common.model.NewsInformation;
import com.webull.information.center.carwler.common.util.jsoup.HtmlBodyPrase;
import com.webull.information.center.carwler.common.util.jsoup.JsoupPraseUtil;

/**
 * 彭博新闻
 * 
 * @author shimingjun
 * @date 2016年8月22日 下午8:25:12
 * @version 1.0
 * @since JDK 1.8
 */
public class Bloomberg_HtmlPrase implements HtmlBodyPrase {
	protected final Logger logger = LogManager.getLogger(getClass());

	/**
	 * for example
	 * :http://news.cmlviz.com/2016/07/21/tivo-inc-and-verint-systems-inc-head-
	 * to-head-compare.html
	 */
	@Override
	public void praseNewsInfo(org.jsoup.nodes.Document doc, NewsInformation info) {
		try {

			// title
			Optional.ofNullable(doc.select("h1.lede-headline>span.lede-headline__highlighted")).map(hi0 -> hi0.first())
					.map(h1 -> StringUtils.stripToNull(h1.html())).ifPresent(h1_0 -> info.setTitle(h1_0));
			// sourceName
			Elements els = doc.select("div.author-byline>span.author-name");
			if (els.isEmpty())
				els = doc.select("div.author-byline>a.author-link");
			if (els.isEmpty())
				els = doc.select("div.author-byline>a.author-twitter");

			Optional.ofNullable(els).map(au0 -> au0.first()).map(au1 -> StringUtils.stripToNull(au1.html()))
					.ifPresent(au1_1 -> info.setSourceName(au1_1));

			// new time
			Optional.ofNullable(doc.select("div.published-info>time")).map(time0 -> time0.first())
					.map(time2 -> StringUtils.stripToNull(time2.attr("datetime"))).ifPresent(pdate -> {
						// datetime="2016-08-01T05:37:25.401Z"
						info.setPushTime(pdate);
						try {
							String day = StringUtils.stripToEmpty(StringUtils.substringBefore(pdate, "T"));
							String time = StringUtils.stripToEmpty(
									StringUtils.substringBeforeLast(StringUtils.substringAfter(pdate, "T"), "."));

							Date d = UtilDate.parse(day + " " + time, 0, "yyyy-MM-dd HH:mm:ss");
							Optional.ofNullable(d).ifPresent(d0 -> info.setNewsTime(d));
						} catch (Exception e) {
						}
					});

			// 新闻正文
			StringBuilder context = new StringBuilder();

			Optional.ofNullable(doc.select("div.article-abstract__item-text")).ifPresent(tex0 -> tex0.forEach(tex1 -> {
				Optional.ofNullable(StringUtils.stripToNull(tex1.html()))
						.ifPresent(text2 -> context.append("<p><string>" + text2 + "</strong></p>"));
			}));

			Optional.ofNullable(doc.select("div.article-body__content").first()).ifPresent(ps0 -> {

				Optional.ofNullable(ps0.select("div>aside")).ifPresent(as -> as.remove());
				Optional.ofNullable(ps0.select("div.ad-v2")).ifPresent(as2 -> as2.remove());
				Optional.ofNullable(ps0.select("div.terminal-tout")).ifPresent(as2 -> as2.remove());

				Optional.ofNullable(ps0.select("a>meta")).ifPresent(as2 -> as2.remove());
				Optional.ofNullable(ps0.select("a")).ifPresent(a0 -> {
					for (int i = a0.size() - 1; i >= 0; i--) {
						JsoupPraseUtil.replaceWithText(a0.get(i));
						// a0.get(i).replaceWith(new TextNode(a0.get(i).html(),
						// ""));
					}
				});
				// for (int i = ps0.size() - 1; i >= 0; i--) {
				// Element p = ps0.get(i);
				// if (StringUtils.isBlank(p.data())) p.remove();
				// }
				Optional.ofNullable(ps0.children()).ifPresent(pss1 -> {
					for (int i = pss1.size() - 1; i >= 0; i--) {
						JsoupPraseUtil.trimParagraph(pss1.get(i));
						if (!pss1.get(i).hasText())
							pss1.get(i).remove();
					}
				}

				);
				info.setContent(context.append(StringUtils.stripToNull(ps0.html())).toString());
			});
		} catch (Exception e) {
			logger.warn(e);
		}
	}

	public static void main(String[] args) throws ParseException, IOException {
		String url2 = "http://www.bloomberg.com/news/articles/2016-08-01/china-said-to-consider-mergers-to-create-two-state-steel-giants-irbltcmf";
		url2 = "http://www.bloomberg.com/news/articles/2016-08-19/u-s-stock-rally-sputters-in-week-as-s-p-500-profit-outlook-dims";
		url2 = "http://www.bloomberg.com/news/articles/2016-08-16/chinese-drugmaker-hid-test-results-that-may-show-contamination?cmpid=google";
		Connection connection = Jsoup.connect(url2).userAgent(
				"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36")
				// .header("x-client-data",
				// "CIq2yQEIpbbJAQjEtskBCP2VygEIwpjKAQjwnMoB")
				.header("x-client-data", RandomStringUtils.randomAlphanumeric(40));

		connection.proxy("127.0.0.1", 1080);
		org.jsoup.nodes.Document doc = connection.timeout(10000).get();
		NewsInformation info = new NewsInformation();
		new Bloomberg_HtmlPrase().praseNewsInfo(doc, info);
		System.out.println(info);

		// Mon Mar 17, 2014 9:46pm EDT
		// Wed Jan 15 00:00:00 CST 2014
		// Date d = UtilDate.parse("Mon Mar 17, 2014 9:46pm", Locale.US,
		// TimeZone.getTimeZone("EDT"), "E MMM dd, yyyy hh:mma");
		// System.out.println(d);

	}
}
